/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.PlayersControl;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * *
 * @author laarguelless
 *
 * Este es el JFrame donde se desarrolla el juego teniendo en cuenta los
 * parámetros de configuración iniciales ..
 */
public class GameFrame extends JFrame {

    private JFrame parent;
    private PlayersControl playersControl;
    private final int MYWIDTH = 600;
    private final int MYHEIGHT = 600;
    private final int ROWS = 30;
    private final int COLS = 30;
    private final int ROWSIZE = MYHEIGHT / ROWS;
    private final int COLSIZE = MYWIDTH / COLS;

    public GameFrame(PlayersControl playersControl, JFrame parent) {
        this.playersControl = playersControl;
        this.playersControl.setSize(ROWS, COLS, ROWSIZE, COLSIZE);
        this.parent = parent;
        init();
    }

    /**
     * Método que initializa los componentes del JFrame
     */
    private void init() {
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setTitle("IPD Simulation");
        setSize(MYWIDTH, MYHEIGHT + 100);
        setVisible(true);
        setResizable(false);
        setLayout(new BorderLayout(2, 2));

        /**
         * Primero se inicializa el JPanel de los botones *
         */
        JPanel buttonPanel = new JPanel();
        buttonPanel.setSize(MYWIDTH, 100);

        JButton startButton = new JButton("Start");
        startButton.setSize(30, 20);
        startButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                startButtonActionPerformed();
            }
        });
        buttonPanel.add(startButton);

        JButton stopButton = new JButton("Stop");
        stopButton.setSize(30, 20);
        stopButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                stopButtonActionPerformed();
            }
        });
        buttonPanel.add(stopButton);

        JButton closeButton = new JButton("close");
        closeButton.setSize(30, 20);
        closeButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                closeButtonActionPerformed();
            }
        });
        buttonPanel.add(closeButton);

        add(buttonPanel, BorderLayout.PAGE_START);

        /**
         * Ahora se inicializa la grilla*
         */
        JPanel gridPanel = playersControl.intiGrid();
        add(gridPanel);
    }

    private void startButtonActionPerformed() {
        Thread thread = new Thread(playersControl);
        thread.start();
    }

    private void stopButtonActionPerformed() {
        playersControl.stop();
    }

    /**
     * Cierra la ventana
     */
    private void closeButtonActionPerformed() {
        parent.setVisible(true);
        dispose();
    }
}
